package com.yunxin.customui.service.handlerimpl.table;

import cn.hutool.core.util.ObjectUtil;
import com.yunxin.customui.bean.table.TdBean;
import com.yunxin.customui.bean.table.HandlerConditionEnum;
import com.yunxin.customui.bean.table.HandlerParam;
import com.yunxin.customui.myinterface.table.ITdHandler;
import org.springframework.stereotype.Service;

/**
 * 改变文字
 *
 * @author LGY
 * @date 2021/08/12
 */
@Service
public class ChangeText implements ITdHandler {


    @Override
    public void handler(TdBean tdBean, HandlerParam param) {

        if (param != null && tdBean.getValue()!=null) {
            try{
                if (param.getHandlerConditionEnum() == HandlerConditionEnum.大于等于) {
                    if (Float.parseFloat(tdBean.getValue()) >= Float.parseFloat(param.getCompareValue())) {
                        tdBean.setValue(param.getParam());
                    }
                }else
                if (param.getHandlerConditionEnum() == HandlerConditionEnum.大于) {
                    try {
                        if (Float.parseFloat(tdBean.getValue()) > Float.parseFloat(param.getCompareValue())) {
                            tdBean.setValue(param.getParam());
                        }
                    }catch (NumberFormatException ignored){
                    }


                }else
                if (param.getHandlerConditionEnum() == HandlerConditionEnum.小于等于) {
                    try {
                        if (Float.parseFloat(tdBean.getValue()) <= Float.parseFloat(param.getCompareValue())) {
                            tdBean.setValue(param.getParam());
                        }
                    }catch (NumberFormatException ignored){
                    }

                }else
                if (param.getHandlerConditionEnum() == HandlerConditionEnum.小于) {
                    if (Float.parseFloat(tdBean.getValue()) < Float.parseFloat(param.getCompareValue())) {
                        tdBean.setValue(param.getParam());
                    }

                }else
                if (param.getHandlerConditionEnum() == HandlerConditionEnum.不等于) {
                    String[] split = param.getCompareValue().split("&");
                    for (int i = 0; i < split.length; i++) {
                        if (ObjectUtil.equal(tdBean.getValue(), split[i])) {
                            return;
                        }
                    }
                    tdBean.setValue(param.getParam());
                }else
                if (param.getHandlerConditionEnum() == HandlerConditionEnum.等于) {
                    try {
                        float v = Float.parseFloat(tdBean.getValue());
                        float v1 = Float.parseFloat(param.getCompareValue());
                        if (v == v1){
                            tdBean.setValue(param.getParam());
                        }
                    }catch (NumberFormatException e){
                        if (ObjectUtil.equal(tdBean.getValue(), param.getCompareValue())) {
                            tdBean.setValue(param.getParam());
                        }
                    }

                }else
                if (param.getHandlerConditionEnum() == HandlerConditionEnum.in前开后开) {
                    String[] split = param.getCompareValue().split("&");
                    float minValue = Float.parseFloat(split[0]);
                    float maxValue = Float.parseFloat(split[1]);
                    if (Float.parseFloat(tdBean.getValue()) < maxValue && Float.parseFloat(tdBean.getValue()) > minValue) {
                        tdBean.setValue(param.getParam());
                    }
                }else
                if (param.getHandlerConditionEnum() == HandlerConditionEnum.in前开后闭) {
                    String[] split = param.getCompareValue().split("&");
                    float minValue = Float.parseFloat(split[0]);
                    float maxValue = Float.parseFloat(split[1]);
                    if (Float.parseFloat(tdBean.getValue()) <= maxValue && Float.parseFloat(tdBean.getValue()) > minValue) {
                        tdBean.setValue(param.getParam());
                    }
                }else
                if (param.getHandlerConditionEnum() == HandlerConditionEnum.in前闭后开) {
                    String[] split = param.getCompareValue().split("&");
                    float minValue = Float.parseFloat(split[0]);
                    float maxValue = Float.parseFloat(split[1]);
                    if (Float.parseFloat(tdBean.getValue()) < maxValue && Float.parseFloat(tdBean.getValue()) >= minValue) {
                        tdBean.setValue(param.getParam());
                    }
                }else
                if (param.getHandlerConditionEnum() == HandlerConditionEnum.in前闭后闭) {
                    String[] split = param.getCompareValue().split("&");
                    float minValue = Float.parseFloat(split[0]);
                    float maxValue = Float.parseFloat(split[1]);
                    if (Float.parseFloat(tdBean.getValue()) <= maxValue && Float.parseFloat(tdBean.getValue()) >= minValue) {
                        tdBean.setValue(param.getParam());
                    }
                }
            }catch (NumberFormatException ignored){
                //跳过文字转换
            }

        }

    }
}
